package com.chujian.Adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Threads.HttpClientGetThread;
import com.chujian.Utils.Constan;
import com.chujian.model.FoodList;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class OrderHistoryDetailConsumerAdapter extends BaseAdapter{
	private List<FoodList>mList;
	private Context context;
	private LayoutInflater mLayoutInflater;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			String str=(String) msg.obj;
			Toast.makeText(context,str, Toast.LENGTH_SHORT).show();
			
		};
	};
	
	public OrderHistoryDetailConsumerAdapter(List<FoodList> mList,
			Context context) {
		super();
		this.mList = mList;
		this.context = context;
		mLayoutInflater=LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder=null;
		if(convertView==null){
			holder=new ViewHolder();
			convertView=mLayoutInflater.inflate(R.layout.order_history_detail_consumer_item, null);
			holder.tvFoodName=(TextView) convertView.findViewById(R.id.tv_order_history_detail_c_item_name);
			holder.tvFoodPrice=(TextView) convertView.findViewById(R.id.tv_order_history_detail_c_item_price);
			holder.tvFoodAmout=(TextView) convertView.findViewById(R.id.tv_order_history_detail_c_item_amount);
			holder.rgZanOrCai=(RadioGroup) convertView.findViewById(R.id.rg_zan_cai);
			holder.rbCai=(RadioButton) convertView.findViewById(R.id.rb_cai);
			holder.rbZan=(RadioButton) convertView.findViewById(R.id.rb_zan);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		convertView.setBackgroundColor(Color.WHITE);
		holder.tvFoodName.setText(mList.get(position).getFoodName());
		holder.tvFoodAmout.setText(mList.get(position).getFoodAmount()+"");
		holder.tvFoodPrice.setText(mList.get(position).getFoodPrice()+"元");
		int zan=mList.get(position).getComment_zan();
		int cai=mList.get(position).getComment_cai();
		 if(zan==0&&cai==1){
			 holder.rgZanOrCai.check(R.id.rb_cai);
			holder.rbCai.setClickable(false);
			holder.rbZan.setClickable(false);
		}else if(zan==1&&cai==0){
			holder.rgZanOrCai.check(R.id.rb_zan);
			holder.rbCai.setClickable(false);
			holder.rbZan.setClickable(false);
		}else if(zan==0&&cai==0){
			
		
		
		holder.rgZanOrCai.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				RadioButton checkRadioButton=(RadioButton) group.findViewById(checkedId);
//				Toast.makeText(context,"id:"+checkedId, Toast.LENGTH_SHORT).show();
				group.findViewById(R.id.rb_zan).setClickable(false);
				group.findViewById(R.id.rb_cai).setClickable(false);
				String url=null;
				switch (checkedId) {
				case R.id.rb_zan:
					url=Constan.BASE_URL+"CommentServlet"+"?categary="+"zan"+"&foodListId="+
				mList.get(position).getFoodListId()+"&food_name="+mList.get(position).getFoodName();
					new HttpClientGetThread(url, mHandler).start();
					break;
				case R.id.rb_cai:
					url=Constan.BASE_URL+"CommentServlet"+"?categary="+"cai"+"&foodListId="+
				mList.get(position).getFoodListId()+"&food_name="+mList.get(position).getFoodName();
					new HttpClientGetThread(url, mHandler).start();
					break;
				default:
					break;
				}
			}
		});
		 }
		
		
		return convertView;
	}
	class ViewHolder{
		public TextView tvFoodName;
		public TextView tvFoodPrice;
		public TextView tvFoodAmout;
		public RadioGroup rgZanOrCai;
		public RadioButton rbZan;
		public RadioButton rbCai;
	}
}
